<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //创建函数 truncate(str, maxlength) 来检查 str 的长度，如果超过 maxlength —— 应使用 "…" 来代替 str 的结尾部分，长度仍然等于 maxlength。
        function truncate(str, maxlength) {
            //如果字符串长度大于maxlength，就把下标从0开始到maxlength的字符给提取出来
            if (str.length > maxlength) {
                return str.slice(0, maxlength - 1) + "...";
            } else {
                return str.slice(0, maxlength);
            }
        }
        console.log(truncate("What I'd like to tell on this topic is:", 20)); // "What I'd like to te…"
        console.log(truncate("Hi everyone!", 20)); // "Hi everyone!"
    </script>
</body>

</html>